﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Data;
using Frame.Lib.Common;

namespace Frame.Lib.Utilities
{
    public class WebUtil
    {
        public static void SetSaveBtn(object flag, Button btnSave)
        {
            if (flag != null)
            {
                btnSave.Text = "更新";                 
            }
        }

        public static void AllowPageScroll(Page page)
        {
            HtmlGenericControl body = (HtmlGenericControl)page.Master.Page.Form.FindControl("body");
            body.Attributes["scroll"] = "auto";
        }
        

         #region 导出Excel
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="gvList"></param>
        /// <param name="fileName"></param>
        public static void ExportExcel(string html,string fileName)
        {
            HttpResponse response = System.Web.HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.Buffer = true;
            response.Charset = "utf-8";

            if (!fileName.EndsWith("xls"))
            {
                fileName += ".xls";
            }

            response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpContext.Current.Server.UrlEncode(fileName));
            response.ContentEncoding = System.Text.Encoding.Default;

            response.ContentType = "application/ms-excel";

            response.Write(html);
            response.End();
        }

        public static void ExportExcelSample()
        {
        }
        #endregion


        /// <summary>
        /// 把枚举值绑定到列表数据控件
        /// </summary>
        /// <param name="drp"></param>
        /// <param name="enumType"></param>
        public static void BindListControlWithEnum(ListControl ctl, Type enumType)
        {
            List<EnumInfo> enumInfos = ConvertUtil.ConvertEnum(enumType);
            ctl.DataSource = enumInfos;
            ctl.DataTextField = "Name";
            ctl.DataValueField = "ID";
            if (enumInfos.Count > 0)
            {
                ctl.SelectedIndex = 0;
            }
            ctl.DataBind();
        }


        public static void SetTimeBox(TextBox ctl, bool showTime)
        {
            ctl.Attributes.Add("readonly", "readonly");
            ctl.Style.Add("cursor", "pointer");
            ctl.Attributes.Add("onclick", "setday(this," + showTime.ToString().ToLower() + ")");
        }
    }
}
